/**
 * 
 */
package com.spaceserviver.misc;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Disposable;

/**
 * @author kot
 * 
 */
public class ImageButton extends Image implements Disposable {

	TextureRegion down;
	TextureRegion up;

	public ClickListener clickListener;

	public ImageButton(TextureRegion down, TextureRegion up) {
		super(up);

		this.down = down;
		this.up = up;

		this.width = up.getRegionWidth();
		this.height = up.getRegionHeight();
	}

	@Override
	public boolean touchDown(float x, float y, int pointer) {
		super.setRegion(down);
		return true;
	}

	@Override
	public void touchUp(float x, float y, int pointer) {
		super.setRegion(up);

		super.touchUp(x, y, pointer);

		boolean isInside = isInsideButton(x, y);
		if (this.clickListener != null && isInside)
			this.clickListener.onClick(this);
	}

	private boolean isInsideButton(float x, float y) {
		if ((x > 0 && x <= this.width) && (y > 0 && y < this.height))
			return true;
		return false;
	}

	public interface ClickListener {
		public void onClick(ImageButton button);
	}

	@Override
	public void dispose() {

	}
}
